<template>
  <a-card :bordered="false" :bodyStyle="{padding:'0px 5px',minHeight:'600px'}">
    <a-tabs @change="tabChange">
      <a-tab-pane key="TaskTree" tab="任务">
        <!-- 操作按钮区域 -->
        <div class="table-operator" v-if="hasPower">
          <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
          <!-- <a-button type="primary" icon="download" @click="handleExportXls('LMIND')">导出</a-button>
          <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
            <a-button type="primary" icon="import">导入</a-button>
          </a-upload> -->
          <a-dropdown v-if="selectedRowKeys.length > 0">
            <a-menu slot="overlay">
              <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
            </a-menu>
            <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
          </a-dropdown>
        </div>
        <task-tree ref="TaskTree" @fn="taskTreeFn" :hasPower="hasPower" :projectCode="projectCode"/>
      </a-tab-pane>
      <a-tab-pane key="TaskCalendar" tab="日历" force-render>
        <task-calendar ref="TaskCalendar" :hasPower="hasPower" :projectCode="projectCode" @fn="taskTreeFn"></task-calendar>
      </a-tab-pane>
    </a-tabs>
    <project-task-modal ref="ProjectTaskModal" @ok="handleOk" :projectCode="projectCode"></project-task-modal>
    <task-time-modal ref="TaskTimeModal"></task-time-modal>
  </a-card>
</template>

<script>
import TaskTree from './modules/TaskTree'
import TaskCalendar from './modules/TaskCalendar'
import ProjectTaskModal from './modal/ProjectTaskModal'
import TaskTimeModal from './modal/TaskTimeModal'
import { getAction, putAction } from '@/api/manage'
import { mapGetters } from 'vuex'
export default {
  name: 'ProjectTask',
  mixins: [],
  components: {
    TaskTree,
    TaskCalendar,
    ProjectTaskModal,
    TaskTimeModal
  },
  data() {
    return {
      selectedRowKeys: [],
      projectCode: '',
      url: {
        queryByCode: '/pms/task/queryByCode',
        edit: '/pms/task/edit'
      },
      projectData: {} // 项目基本信息  权限控制用
    }
  },
  computed: {
    hasPower() { // 判断是否为项目经理或助手
      const username = this.userInfo().username
      const projectManager = this.projectData.projectManager ? this.projectData.projectManager.split(',') : []
      const projectAssistant = this.projectData.projectAssistant ? this.projectData.projectAssistant.split(',') : []
      const isProjectManager = projectManager.indexOf(username) >= 0
      const isProjectAssistant = projectAssistant.indexOf(username) >= 0
      if (isProjectManager || isProjectAssistant) {
        return true
      } else {
        return false
      }
    }
  },
  methods: {
    ...mapGetters(['userInfo']),
    handleAdd() {
      this.$refs.ProjectTaskModal.add()
    },
    handleEdit(data) {
      this.$refs.ProjectTaskModal.edit(data)
    },
    handleDetail(data) {
      this.$refs.ProjectTaskModal.disableSubmit = true
      this.$refs.ProjectTaskModal.edit(data)
    },
    addChild(data) {
      this.$refs.ProjectTaskModal.addChild(data)
    },
    setTaskTime(data) {
      this.$refs.TaskTimeModal.show(data.taskCode)
    },
    taskCheck(data) {
      this.$refs.ProjectTaskModal.isCheck = true
      this.$refs.ProjectTaskModal.disableSubmit = true
      this.$refs.ProjectTaskModal.edit(data)
    },
    handleOk() {
      this.$refs.TaskTree.loadData()
      this.$refs.TaskCalendar.loadData()
    },
    async handelStart(data) {
      if (!data.preTaskCode) {
        data.taskStatus = '1'
        this.editTask(data)
        return false
      }
      try {
        const res = await getAction(this.url.queryByCode, { code: data.preTaskCode })
        if (!res.success) {
          this.$message.warning(res.message)
          return false
        }
        if (res.result.taskStatus !== '3') {
          this.$message.warning('前置任务未完成，不允许开始')
          return false
        }
        data.taskStatus = '1'
        this.editTask(data)
      } catch (err) {
        console.log(err)
      }
    },
    editTask(data) {
      putAction(this.url.edit, data).then(res => {
        if (!res.success) {
          this.$message.warning(res.message)
          return false
        }
        this.$message.success('任务已开始')
      })
    },
    taskTreeFn(type, data) {
      switch (type) {
        case 'edit':
          this.handleEdit(data)
          break
        case 'addChild':
          this.addChild(data)
          break
        case 'taskTime':
          this.setTaskTime(data)
          break
        case 'check':
          this.taskCheck(data)
          break
        case 'start':
          this.handelStart(data)
          break
        case 'detail':
          this.handleDetail(data)
          break
      }
    },
    tabChange(activeKey) {
      this.$refs[activeKey].loadData()
    },
    loadProjectData() {
      getAction('pms/project/list', { projectCode: this.projectCode }).then(res => {
        if (res.success) {
          this.projectData = res.result.records ? res.result.records[0] : {}
        } else {
          this.$message.warning(res.message)
        }
      })
    }
  },
  created() {
    this.projectCode = this.$route.fullPath.replace('/project/ProjectTask/', '')
    // this.projectCode = this.$route.params.code
    if (this.projectCode && this.projectCode !== '/') {
      this.loadProjectData()
    }
  }
}
</script>
<style scoped>
.ant-card-body .table-operator{
  margin-bottom:0px;
}
  @import '~@assets/less/common.less';
</style>
